package com.cgbl.cg.ui.work

import android.os.Bundle
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.PhotoAdapter
import com.cgbl.cg.databinding.AppActivityReleaseWorkBinding
import com.cgbl.cg.view.FinishPop
import com.cgbl.cg.view.ImagePop
import com.cgbl.cg.vm.work.ReleaseWorkViewModel
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.SmartGlideImageLoader
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CreateWorkBean
import com.robot.base.model.WorksBean
import com.robot.base.util.ChoiceImgHelper
import com.robot.base.util.LiveDataBus

@Route(path = AppConstants.Router.WORK.A_RELEASE_WORK)
class ReleaseWorkActivity : GLBaseActivity<AppActivityReleaseWorkBinding, ReleaseWorkViewModel>() {
    private lateinit var photoAdapter: PhotoAdapter

    private var draftWorks: WorksBean? = null

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_release_work

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        addSingleTitleBar(getString(R.string.app_en_tips_124))
        initAdapter()

        draftWorks?.apply {
            viewModel.name.set(workName)
            viewModel.direction.set(workExplain)
            images?.forEach {
                viewModel.imgList.add(it.filePath ?: "")
                photoAdapter.addData(0, it.filePath ?: "")
            }
        }
    }

    override fun onBackPressed() {
//        super.onBackPressed()
        val pop = FinishPop(this)
        XPopup.Builder(this).asCustom(pop).show()
        pop.setConfirmClickListener {
            viewModel.createWork()
        }
        pop.setCancelClickListener {
            finish()
        }
    }

    override fun initParam() {
        draftWorks =
            intent.getSerializableExtra(AppConstants.BundleKey.RELEASE_DRAFT_WORKS) as WorksBean?
    }

    private fun initAdapter() {
        photoAdapter = PhotoAdapter(R.mipmap.app_ic_photo_add)
        binding.mRvImg.apply {
            adapter = photoAdapter
        }
        photoAdapter.addData("")
        photoAdapter.addChildClickViewIds(R.id.item_iv_img, R.id.item_iv_close)

        photoAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.item_iv_img -> {
                    if (TextUtils.isEmpty(photoAdapter.data[position])) {
                        ChoiceImgHelper.ChoiceImg(this, 9, {
                            it.forEach {
                                viewModel.doUpload(it?.fileName ?: "", it?.compressPath ?: "") {
                                    photoAdapter.addData(0, it)
                                }

                            }
                        }, {

                        })
                    } else {
                        val list = mutableListOf<String>()
                        photoAdapter.data.forEach {
                            if (!TextUtils.isEmpty(it)) list.add(it)
                        }
                        val imgPop = ImagePop(this).isInfinite(true).isShowSaveButton(false)
                            .setXPopupImageLoader(SmartGlideImageLoader())
                            .setSrcView(view.findViewById(R.id.item_iv_img), position)
                            .setImageUrls(list.toMutableList())
                        XPopup.Builder(this).asCustom(imgPop).show()
                    }
                }
                R.id.item_iv_close -> {
                    viewModel.imgList.remove(photoAdapter.data[position])
                    photoAdapter.data.removeAt(position)
                }
            }
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        LiveDataBus.get().with("WorkData", CreateWorkBean::class.java).observe(this) {
            viewModel.type = it.type
            viewModel.imgUrl = it.imgUrl
            viewModel.save = it.save
            viewModel.tagList = it.tagList
            viewModel.declare = it.declare
            viewModel.copyrightType = it.copyrightType
        }
    }
}